package org.ttitfly.zuoyue.service.workflow;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.ttitfly.workflow.Session;
import org.ttitfly.workflow.SessionEstablishment;
import org.ttitfly.workflow.engine.persistence.PersistenceService;
import org.ttitfly.workflow.engine.persistence.hibernate.HibernateTaskDao;
import org.ttitfly.workflow.engine.run.Task;
import org.ttitfly.workflow.exception.NoSessionConnectedException;

@Component
public class TaskManager {

	@Resource
	private SessionEstablishment sessionEstablishment;
	
	@Resource(name = "hibernatePersistenceService")
	private PersistenceService persistenceService;
	
	@Resource
	private HibernateTaskDao hibernateTaskDao;

	/**
	 * 分页列出当前用户的待处理任务
	 * 
	 * @return
	 */
	public Page<Task> findRunningTask(String processName, String activityName,
			int processInstanceId, int pageNo, int pageSize) {
	/*
		if (pageNo < 1) {
			pageNo = 1;
		}
		if (pageSize < 1) {
			pageSize = 20;
		}
	*/
		Page<Task> page = new Page<Task>();
		Session session = sessionEstablishment.connect(getUserId(), null);
		int total = session.countRunningTask(processName, activityName, processInstanceId);
		page.setTotalCount(total);
		int start = (pageNo - 1) * pageSize;
		if (start < total) {
			// 总数大于开始下标才查询
			page.setResult(session.findRunningTask(processName, activityName, processInstanceId, start, pageSize));
		}
		try {
			session.close();
		} catch (NoSessionConnectedException e) {
		}
		return page;
	}
	
	/**
	 * 分页列出当前用户的已处理任务
	 * 
	 * @return
	 */
	public Page<Task> findClosedTask(String processName, String activityName,
			int processInstanceId, int pageNo, int pageSize) {
	/*
		if (pageNo < 1) {
			pageNo = 1;
		}
		if (pageSize < 1) {
			pageSize = 10;
		}
	*/
		Page<Task> page = new Page<Task>();
		Session session = sessionEstablishment.connect(getUserId(), null);
		int total = session.countClosedTask(processName, activityName, processInstanceId);
		page.setTotalCount(total);
		int start = (pageNo - 1) * pageSize;
		if (start < total) {
			// 总数大于开始下标才查询
			page.setResult(session.findClosedTask(processName, activityName, processInstanceId, start, pageSize));
		}
		try {
			session.close();
		} catch (NoSessionConnectedException e) {
		}
		return page;
	}
	
	public Task get(int id) {
		return persistenceService.getTask(id);
	}
	
	@Transactional
	public boolean removeTask(int processInstanceId){
		return hibernateTaskDao.removeTask(processInstanceId);
	}
	
	@Transactional
	public boolean removeProcessInstance(int processInstanceId){
		return hibernateTaskDao.removeProcessInstance(processInstanceId);
	}
	
	public List<Task> listHistory(int processInstanceId) {
		return hibernateTaskDao.findClosedTask(processInstanceId);
	}

	
	public List<Task> listRuning(int processInstanceId){
		return hibernateTaskDao.findRunningTask(processInstanceId);
	}
	
	private String getUserId() {
		return SpringSecurityUtils.getCurrentUserName();
	}

	public List<Task> getTaskByClosedTime(String closedTime){
		return hibernateTaskDao.findTaskByClosedTime(closedTime);
	}
}
